package com.sls.game.service.impl;

import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.core.util.ServerDateAndTime;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.jpay.JpayAreaDAO;
import com.sls.game.dao.jpay.JpayGoodsDAO;
import com.sls.game.page.Pagination;
import com.sls.game.pojo.jieyitong.JieyitongAreaServerInfo;
import com.sls.game.pojo.jieyitong.JieyitongGoodsInfo;
import com.sls.game.service.JpayService;

@Service("jpayService")
public class JpayServiceImpl implements JpayService {

	@Resource(name = "jpayGoodsDAO")
	private JpayGoodsDAO jpayGoodsDAO;
	@Resource(name = "jpayAreaDAO")
	private JpayAreaDAO jpayAreaDAO;
	private Gson gson;

	public JpayServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public void saveGoodsInfo(String body, Head head) {
		JieyitongGoodsInfo jInfo = gson
				.fromJson(body, JieyitongGoodsInfo.class);
		this.validate(jInfo);
		this.validateFeild(jInfo);
		try {
			JieyitongGoodsInfo jpay = this.jpayGoodsDAO.queryByGoodsId(jInfo
					.getGoodsId());
			if (jpay != null) {
				throw new ServerDisponseException(
						ExceptionCodeDes.GOODS_EXIST.getCode(),
						ExceptionCodeDes.GOODS_EXIST.getDescription());
			}
			jInfo.setId(UUID.randomUUID().toString());
			jInfo.setIsAudit("0");
			jInfo.setInsertTime(ServerDateAndTime.getNowDateTimeOfFormat());
			this.jpayGoodsDAO.insert(jInfo);
		} catch (Exception e) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}

	}

	@Override
	public void updateGoodsInfo(String body, Head head) {
		JieyitongGoodsInfo jInfo = gson
				.fromJson(body, JieyitongGoodsInfo.class);
		this.validate(jInfo);
		try {
			jInfo.setIsAudit("0");
			jInfo.setModifyTime(ServerDateAndTime.getNowDateTimeOfFormat());
			this.jpayGoodsDAO.update(jInfo);
		} catch (Exception e) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}

	}

	@Override
	public JieyitongGoodsInfo queryById(String body, Head head) {
		String id = gson.fromJson(body, String.class);
		JieyitongGoodsInfo jInfo = null;
		try {
			jInfo = this.jpayGoodsDAO.queryById(id);
		} catch (Exception e) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		return jInfo;
	}

	@Override
	public Pagination<JieyitongGoodsInfo> queryPagination(String body, Head head) {
		JieyitongGoodsInfo jInfo = gson
				.fromJson(body, JieyitongGoodsInfo.class);
		this.validate(jInfo);
		Pagination<JieyitongGoodsInfo> page = null;
		try {
			page = this.jpayGoodsDAO.queryPaginationList(jInfo);
		} catch (Exception e) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		return page;
	}

	@Override
	public void saveAreaInfo(String body, Head head) {
		JieyitongAreaServerInfo areaSrvInfo = gson.fromJson(body,
				JieyitongAreaServerInfo.class);
		this.validate(areaSrvInfo);
		try {
			this.jpayAreaDAO.insert(areaSrvInfo);
		} catch (Exception e) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}

	}

	@Override
	public Pagination<JieyitongAreaServerInfo> queryAreaPagination(String body,
			Head head) {
		JieyitongAreaServerInfo areaSrvInfo = gson.fromJson(body,
				JieyitongAreaServerInfo.class);
		this.validate(areaSrvInfo);
		Pagination<JieyitongAreaServerInfo> pagination = null;
		try {
			pagination = this.jpayAreaDAO.queryPaginationList(areaSrvInfo);
		} catch (Exception e) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		return pagination;
	}

	/**
	 * 校验是否为null
	 * 
	 * @param obj
	 * @throws ServerDisponseException
	 */
	private void validate(Object obj) {
		if (obj == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getDescription());
		}
	}

	private void validateFeild(JieyitongGoodsInfo jInfo) {
		if (StringUtils.isEmpty(jInfo.getGoodsId())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PRODCODE_EMPTY.getCode(),
					ExceptionCodeDes.PRODCODE_EMPTY.getDescription());
		} else if (StringUtils.isEmpty(jInfo.getGoodsName())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.EN_LINK_PRODNAME_EMPTY.getCode(),
					ExceptionCodeDes.EN_LINK_PRODNAME_EMPTY.getDescription());
		} else if (StringUtils.isEmpty(jInfo.getInPrice())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_SUPPLY_PRICE_NULL.getCode(),
					ExceptionCodeDes.GOODS_SUPPLY_PRICE_NULL.getDescription());
		} else if (StringUtils.isEmpty(jInfo.getParValue())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_PARVALUE_NULL.getCode(),
					ExceptionCodeDes.GOODS_PARVALUE_NULL.getDescription());
		} else if (StringUtils.isEmpty(jInfo.getRechargeType())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.RECHARGE_MODE_EMPTY.getCode(),
					ExceptionCodeDes.RECHARGE_MODE_EMPTY.getDescription());
		}
	}

}
